package com.hbx.educms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbx.commonutils.R;
import com.hbx.educms.entity.CrmBanner;
import com.hbx.educms.service.CrmBannerService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author hbx
 * @since 2023-10-12
 */
@Api(tags = {"后台轮播图管理"})
@RestController
//@CrossOrigin
@RequestMapping("/educms/banneradmin")
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    /**
     * 查询所有
     * @param
     * @param
     * @return
     */
    @GetMapping("/getAllBanner")
    public R getAllBanner(){
        LambdaQueryWrapper<CrmBanner> query = new LambdaQueryWrapper<>();
        query.orderByDesc(CrmBanner::getGmtCreate);
        List<CrmBanner> list = bannerService.list(query);
        return R.ok().data("items",list);
    }

    /**
     * 添加轮播图
     * @param banner
     * @return
     */
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner banner){
        bannerService.save(banner);
        return R.ok();
    }

    @GetMapping("/getBanner/{id}")
    public R getBanner(@PathVariable String id){
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("banner",banner);
    }

    /**
     * 修改轮播图
     * @param banner
     * @return
     */
    @PutMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner banner){
        bannerService.updateById(banner);
        return R.ok();
    }

    /**
     * 删除轮播图
     * @param id
     * @return
     */
    @DeleteMapping("/removeBanner/{id}")
    public R removeBanner(@PathVariable String id){
        bannerService.removeById(id);
        return R.ok();
    }
}

